﻿using System;
using WeManageService;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using WeManageForm.Report;
using System.Collections;

namespace WeManageForm
{
    public partial class MainForm : Form
    {
        public colorLabel[,] colorLabels = new colorLabel[53, 7];
        public TipLabel[,] tipLabels = new TipLabel[53, 7];
        public int startX = 0;
        public int startY = 3;

        int ControlIndex = 0;
        bool logout = false;
        public Point mouseDownPoint;
        public MainForm()
        {
            Opacity = 0;
            InitializeComponent();            
            Image image = PicUser.BackgroundImage;
            image = CutEllipse(image, new Rectangle(0, 0, 186, 186), new Size(186, 186), new Size(92,92));
            PicUser.BackgroundImage = image;
            LblDate.Text = DateTime.Now.ToShortDateString();
            LblWeek.Text = DateTime.Now.DayOfWeek.ToString();
            HomePlanForm homePlanForm = new HomePlanForm { TopLevel = false, FormBorderStyle = FormBorderStyle.None };//要添加的Form不显示窗体头和边框
            homePlanForm.Location = new Point(-3, 0);
            FLPDisplay.Controls.Add(homePlanForm);//把form里的内容添到panel中
            homePlanForm.Show();//显示内容 
            LblName.Text = Global.UserName;
            for(int i = 0; i < 53; i++)
            {
                for(int j = 0;j < 7; j++)
                {
                    colorLabels[i, j] = new colorLabel();
                }
            }
            LoadColorBlock();
            //new Thread(LoadControls).Start();
            PlanManager.notifier.StartNotice += ShowNoticeForm;
            PlanManager.notifier.StartAutoFinish += PlanManager.AutoFinish;
            PlanManager.notifier.StartTimer(Global.UserName);
            DelayRecorder.GetRecord();
            DelayRecorder.StartTimer();
            CMSIcon.Opacity = 0.65;
            CMSUser.Opacity = 0.65;
            Opacity = 1;
            DreamLabel();
        }

        ~MainForm()
        {
            DelayRecorder.WriteRecord();
        }

        public class colorLabel : Label
        {
            public int Plannumber
            {
                get
                {
                    return plannumber;
                }
                set
                {
                    plannumber = value;
                    refreshColor();
                }
            }
            private int plannumber;
            public int Year { get; set; }
            public int Month { get; set; }
            public int Day { get; set; }
            public int indexI { get; set; }
            public int indexJ { get; set; }           
            public colorLabel()
            {
                this.indexI = 0;
                this.indexJ = 0;
                this.Plannumber = 0;
                this.Year = 0;
                this.Month = 0;
                this.Day = 0;
                this.Size = new System.Drawing.Size(15, 15);               
                this.BorderStyle = System.Windows.Forms.BorderStyle.None;
                this.Margin = new System.Windows.Forms.Padding(3, 3, 0, 0);
                this.refreshColor();
            }
            public void refreshColor()
            {
                if(this.Plannumber == 0)
                {
                    this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
                }
                if(this.Plannumber > 0 && this.Plannumber <= 2)
                {
                    this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
                }
                if (this.Plannumber > 2 && this.Plannumber <= 4)
                {
                    this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128)))));
                }
                if (this.Plannumber > 4 && this.Plannumber <= 6)
                {
                    this.BackColor = System.Drawing.Color.Gray;
                }
                if (this.Plannumber > 6 && this.Plannumber <= 8)
                {
                    this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                }
            }
        }

        public class TipLabel : Label
        {
            public int Plannumber { get; set; }
            public int Year { get; set; }
            public int Month { get; set; }
            public int Day { get; set; }
            public TipLabel(colorLabel colorLabel)
            {
                this.BorderStyle = BorderStyle.FixedSingle;
                this.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                this.AutoSize = true;
                this.Plannumber = colorLabel.Plannumber;
                this.Year = colorLabel.Year;
                this.Month = colorLabel.Month;
                this.Day = colorLabel.Day;
                this.BackColor = Color.White;
                this.Text = this.Plannumber.ToString() + "个任务：" + this.Year + "-" + this.Month + "-" + this.Day;
            }
        }

        private void LoadControls()
        {
            Opacity = 0;
            Thread.Sleep(900);
            Opacity = 1;
        }

        private void BtnHome_Click(object sender, EventArgs e)
        {
            if(ControlIndex != 0)
            {
                ControlIndex = 0;
                this.BtnHome.BackColor = System.Drawing.SystemColors.ControlDarkDark;
                this.BtnTaskManager.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnIntegral.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnReport.BackColor = System.Drawing.SystemColors.ActiveCaption;
                FLPDisplay.Controls.Clear();
                HomePlanForm homePlanForm = new HomePlanForm { TopLevel = false, FormBorderStyle = FormBorderStyle.None };//要添加的Form不显示窗体头和边框
                homePlanForm.Location = new Point(-3, 0);
                FLPDisplay.Controls.Add(homePlanForm);//把form里的内容添到panel中
                homePlanForm.Show();//显示内容 
            }
        }

        private void BtnTaskManager_Click(object sender, EventArgs e)
        {
            if (ControlIndex != 1)
            {
                this.BtnHome.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnTaskManager.BackColor = System.Drawing.SystemColors.ControlDarkDark;
                this.BtnIntegral.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnReport.BackColor = System.Drawing.SystemColors.ActiveCaption;
                ControlIndex = 1;
                FLPDisplay.Controls.Clear();
                PlanManageForm planManageForm = new PlanManageForm { TopLevel = false, FormBorderStyle = FormBorderStyle.None };//要添加的Form不显示窗体头和边框
                FLPDisplay.Controls.Add(planManageForm);//把form里的内容添到panel中
                planManageForm.Show();//显示内容 
            }
        }
        
        private void BtnIntegral_Click(object sender, EventArgs e)
        {
            if (ControlIndex != 2)
            {
                this.BtnHome.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnTaskManager.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnIntegral.BackColor = System.Drawing.SystemColors.ControlDarkDark;
                this.BtnReport.BackColor = System.Drawing.SystemColors.ActiveCaption;
                ControlIndex = 2;
                FLPDisplay.Controls.Clear();
                WishListForm wishListForm = new WishListForm { TopLevel = false, FormBorderStyle = FormBorderStyle.None };//要添加的Form不显示窗体头和边框
                FLPDisplay.Controls.Add(wishListForm);//把form里的内容添到panel中
                wishListForm.Show();//显示内容 
            }
        }

        private void BtnReport_Click(object sender, EventArgs e)
        {
            if (ControlIndex != 3)
            {
                this.BtnHome.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnTaskManager.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnIntegral.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnReport.BackColor = System.Drawing.SystemColors.ControlDarkDark;
                ControlIndex = 3;               
                FLPDisplay.Controls.Clear();
                ReportForm reportForm = new ReportForm() { TopLevel = false, FormBorderStyle = FormBorderStyle.None };//要添加的Form不显示窗体头和边框
                FLPDisplay.Controls.Add(reportForm);//把form里的内容添到panel中
                reportForm.Show();//显示内容 
            }
        }

        private void BtnCurriculum_Click(object sender, EventArgs e)
        {
            if (ControlIndex != 4)
            {
                this.BtnHome.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnTaskManager.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnIntegral.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.BtnReport.BackColor = System.Drawing.SystemColors.ActiveCaption;
                ControlIndex = 4;
                /**/
            }
        }

        private void MainForm_FormClosing(object sender,CancelEventArgs e)
        {
            NotifyIcon.Dispose();
            if (!logout)
            {
                System.Environment.Exit(0);                
            }
        }

        private void BtnLogout_Click(object sender, EventArgs e)
        {
            logout = true;
            this.Close();
            LoginForm loginForm = new LoginForm();
            loginForm.Show();
        }

        private void 打开主页版ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Opacity = 0;            
            //new Thread(LoadControls).Start();
            this.ShowInTaskbar = true; //显示在系统任务栏
            this.WindowState = FormWindowState.Normal;
            this.BringToFront();
            Thread.Sleep(1000);
            Opacity = 1;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            logout = false;
            this.Close();
            NotifyIcon.Dispose();
            System.Environment.Exit(0);
        }

        private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                Opacity = 0;
                this.ShowInTaskbar = true; //显示在系统任务栏 
                this.WindowState = FormWindowState.Normal; //还原窗体
                this.BringToFront();
                Thread.Sleep(1000);
                Opacity = 1;                
            }
        }        

        private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    Opacity = 0;                    
                    this.ShowInTaskbar = true; //显示在系统任务栏 
                    this.WindowState = FormWindowState.Normal; //还原窗体
                    this.BringToFront();
                    Thread.Sleep(1000);
                    Opacity = 1;                    
                }
            }           
        }        

        private void ShowNoticeForm(NoticeArgs args)
        {
            if (args.Plans.Count > 0)
            {
                PlanNotifyForm noticeForm = new PlanNotifyForm(args.Plans,args.State);
                noticeForm.StartPosition = FormStartPosition.CenterScreen;
                noticeForm.ShowDialog();
            }
        }

        private Image CutEllipse(Image img, Rectangle rec, Size size, Size newSize)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
                {
                    br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.FillEllipse(br, new Rectangle(Point.Empty, size));
                }
            }
            bitmap = ZoomImage(bitmap, newSize);
            return bitmap;
        }

        private Bitmap ZoomImage(Bitmap bitmap, Size newSize)
        {
            try
            {
                int destHeight = newSize.Height;
                int destWidth = newSize.Width;
                System.Drawing.Image sourImage = bitmap;
                int width = 0, height = 0;
                //按比例缩放             
                int sourWidth = sourImage.Width;
                int sourHeight = sourImage.Height;
                if (sourHeight > destHeight || sourWidth > destWidth)
                {
                    if ((sourWidth * destHeight) > (sourHeight * destWidth))
                    {
                        width = destWidth;
                        height = (destWidth * sourHeight) / sourWidth;
                    }
                    else
                    {
                        height = destHeight;
                        width = (sourWidth * destHeight) / sourHeight;
                    }
                }
                else
                {
                    width = sourWidth;
                    height = sourHeight;
                }
                Bitmap destBitmap = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage(destBitmap);
                g.Clear(Color.Transparent);
                //设置画布的描绘质量           
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
                g.Dispose();
                //设置压缩质量       
                System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
                long[] quality = new long[1];
                quality[0] = 100;
                System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[0] = encoderParam;
                sourImage.Dispose();
                return destBitmap;
            }
            catch
            {
                return bitmap;
            }
        }

        private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            logout = true;
            this.Close();
            NotifyIcon.Dispose();
            LoginForm loginForm = new LoginForm();
            loginForm.Show();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void MainForm_Resize(object sender, EventArgs e)
        {

        }
        
        //作用 加快界面加载 解决闪屏
        protected override CreateParams CreateParams 
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }

        private void BtnMinSize_Click(object sender, EventArgs e)
        {
            logout = false;
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            logout = false;
            this.Close();
            NotifyIcon.Dispose();
            System.Environment.Exit(0);
        }

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDownPoint = new Point(e.X, e.Y);
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mouseDownPoint.X,
                    this.Location.Y + e.Y - mouseDownPoint.Y);
            }
        }

        private void DreamLabel()
        {
            string[] saying = new string[] {"约定好了的事情就要做到哦","侯非侯王非王，果冻我要喜之郎",
                "虽然我走得很慢，但我从不后退!","前天看见了小鹿，昨天看见了兔子，今天遇到了你"
            ,"今天，也是充满希望的一天"};
            Random rd = new Random();
            int rdi = rd.Next();
            int sayingCount = saying.Count();
            int i = rdi % sayingCount;

            LblWord.Text = saying[i];
        }

        private void PicUser_Click(object sender, EventArgs e)
        {

        }

        private void BtnHelp_Click(object sender, EventArgs e)
        {
            if (BtnHelp.Text == "﹀")
            {
                BtnHelp.Text = "︿";
            }
            if (BtnHelp.Text == "︿")
            {
                BtnHelp.Text = "﹀";
            }          
        }

        private void LblKawayi_Click(object sender, EventArgs e)
        {

        }

        private void colorLabel_MouseHover(object sender, EventArgs e)
        {
            colorLabel colorLabel = (colorLabel)sender;
            int i = colorLabel.indexI;
            int j = colorLabel.indexJ;
            int X = colorLabel.Location.X + 67;
            int Y = colorLabel.Location.Y + 4;
            if (i >= 48)
            {
                X = colorLabel.Location.X - 67;
            }
            if (i == 52)
            {
                X = colorLabel.Location.X - 67;
                Y = colorLabel.Location.Y - 31 + 4;
            }
            tipLabels[i, j] = new TipLabel(colorLabel);
            tipLabels[i, j].Location = new Point(X,Y);
            PanelColorBlock.Controls.Add(tipLabels[i, j]);
            tipLabels[i, j].BringToFront();
        }

        private void colorLabel_MouseLeave(object sender, EventArgs e)
        {
            colorLabel colorLabel = (colorLabel)sender;
            PanelColorBlock.Controls.Remove(tipLabels[colorLabel.indexI, colorLabel.indexJ]);
        }

        private void LoadColorBlock()
        {
            for (int i = 1; i <= 12; i++)
            {
                setBlock(i, startX, startY);
            }
            for (int j = 0; j < 7; j++)
            {
                for (int i = 0; i < 52; i++)
                {
                    colorLabels[i, j].indexI = i;
                    colorLabels[i, j].indexJ = j;
                    FLPColorBlock.Controls.Add(colorLabels[i, j]);
                    colorLabels[i, j].MouseHover += new System.EventHandler(colorLabel_MouseHover);
                    colorLabels[i, j].MouseLeave += new System.EventHandler(colorLabel_MouseLeave);
                }
            }
            colorLabels[52, 0].indexI = 52;
            colorLabels[52, 0].indexJ = 0;
            colorLabels[52, 0].Location = new Point(1007,34);
            colorLabels[52, 0].MouseHover += new System.EventHandler(colorLabel_MouseHover);
            colorLabels[52, 0].MouseLeave += new System.EventHandler(colorLabel_MouseLeave);
            colorLabels[52, 0].Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
            PanelColorBlock.Controls.Add(colorLabels[52, 0]);

            colorLabels[52, 1].indexI = 52;
            colorLabels[52, 1].indexJ = 1;
            colorLabels[52, 1].Location = new Point(1007,52);
            colorLabels[52, 1].MouseHover += new System.EventHandler(colorLabel_MouseHover);
            colorLabels[52, 1].MouseLeave += new System.EventHandler(colorLabel_MouseLeave);
            colorLabels[52, 1].Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
            PanelColorBlock.Controls.Add(colorLabels[52, 1]);
        }
        
        private void setBlock(int month,int startX,int startY)
        {
            //January   (startX,startY)~(endX,endY) length(31)
            //(0,3)~() 31
            for(int i = 0; i < 3; i++)
            {
                colorLabels[0, i].Year = 2019;
                colorLabels[0, i].Month = 12;
                colorLabels[0, i].Day = i+1;
            }
            int startDay = 1;
            int endDay = 31;
            switch (month)
            {
                case 2:
                    endDay = 29;
                    break;
                case 4:
                    endDay = 30;
                    break;
                case 6:
                    endDay = 30;
                    break;
                case 9:
                    endDay = 30;
                    break;
                case 11:
                    endDay = 30;
                    break;
            }
            bool end = false;
            for(int x = startX; x < 53; x++)
            {
                if (end)
                {
                    break;
                }
                int y = 0;
                if (x == startX) y = startY;                
                for (; y < 7; y++)
                {
                    if (startDay > endDay)
                    {
                        this.startX = x;
                        this.startY = y;
                        end = true;
                        break;
                    }                    
                    colorLabels[x, y].Year = 2020;
                    colorLabels[x, y].Month = month;
                    colorLabels[x, y].Day = startDay;

                    DateTime startTime = new DateTime(2020, month, startDay, 0, 0, 0);
                    colorLabels[x, y].Plannumber = Reporter.GetFinishPlanByTime(startTime, startTime.AddDays(1));
                    startDay++;
                }
            }
        }
                
        private void 最小化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            logout = false;
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
